package com.xbao.core.util.pub.tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

public class StringUtils {

	/**
	 * 鎶婂瓧绗︿覆杞崲鎴怘ashmap锛屽瓧绗︿覆姣忕粍鏁版嵁鐢ㄩ�鍙烽殧寮�紝姣忕粍鏁版嵁鐨刱ey鍜寁alue鐢ㄧ瓑鍙风浉鍏宠仈
	 * 
	 * @Methods Name stringToHashmap
	 * @Create In 2012-11-19 By hongliang.gao
	 * @param str
	 * @return HashMap<Object,Object>
	 */
	public static HashMap<Object, Object> stringToHashmap(String str) {
		HashMap<Object, Object> strMap = new HashMap<Object, Object>();
		String[] keyValueArr = str.split(",");
		for (int i = 0; i < keyValueArr.length; i++) {
			String[] keyValue = keyValueArr[i].split("=");
			if (keyValue[0] != null) {
				String value = "";
				if (keyValue.length == 2) {
					value = keyValue[1];
				}
				strMap.put(keyValue[0].trim(), value);
			}
		}
		return strMap;
	}

	/**
	 * 
	 * @Methods Name 鏇挎崲瀛楃涓蹭腑{n}鏍煎紡鐨勫弬鏁�
	 * @Create In 2012-7-12 By hongliang.gao
	 * @param template
	 *            琚浛鎹㈢殑瀛楃涓�
	 * @param values
	 *            鏇挎崲鐨勫弬鏁�
	 * @return String
	 */
	public static String replaceFormatString(String template, String... values) {
		int length = values.length;
		for (int i = 0; i < length; i++) {
			String pattern = "{" + i + "}";
			String value = values[i];
			if (StringUtils.isNull(value))
				value = "";
			template = template.replace(pattern, value);
		}
		return template;
	}
	

	/**
	 * 
	 * @Methods Name 闅忔満鐢熸垚瀛楃锛屽惈澶у啓銆佸皬鍐欍�鏁板瓧
	 * @Create In 2012-7-12 By hongliang.gao
	 * @param length
	 *            鐢熸垚闅忔満鏁扮殑闀垮害
	 * @return String 闅忔満瀛楃涓�
	 */
	public static String getRandomString(int length) {

		String randStr = "";
		for (int i = 0; i < length; i++) {

			int index = (int) Math.round(Math.random() * 2);
			String randChar = "";
			switch (index) {
			case 0:// 澶у啓瀛楃
				randChar = String
						.valueOf((char) Math.round(Math.random() * 25 + 65));
				break;
			case 1:// 灏忓啓瀛楃
				randChar = String
						.valueOf((char) Math.round(Math.random() * 25 + 97));
				break;
			default:// 鏁板瓧
				randChar = String.valueOf(Math.round(Math.random() * 9));
				break;
			}
			randStr += randChar;
		}
		return randStr;
	}

	public static String getbytes(String str) {
		byte[] d = str.getBytes();
		StringBuffer sb = new StringBuffer("");
		for (byte b : d) {
			sb.append(b + "_");
		}
		String result = sb.toString().substring(0, sb.toString().length() - 1);
		return result;
	}

	public static String getbytestoString(String str) {
		String[] s = str.split("_");
		byte[] b = new byte[s.length];
		for (int i = 0; i < s.length; i++) {
			b[i] = Byte.parseByte(s[i]);
		}
		return new String(b);
	}

	/**
	 * 锟斤拷锟斤拷锟斤拷转锟斤拷为锟斤拷值锟斤拷锟街凤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷,转锟斤拷为锟斤拷-65_-86_-73_-94_-69_-73_-66_-77
	 * 
	 * @param str
	 * @return
	 */
	public static String farmmatStringToNum(String str) {
		if (str == null)
			return null;
		byte[] bytes = null;
		try {
			bytes = str.getBytes("gb2312");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result = new String();
		if(bytes==null) return null;
		for (int i = 0, num = bytes.length; i < num; i++) {
			result = result + bytes[i];
			if (i < num - 1)
				result = result + "_";
		}
		return "@" + result + "@";
	}

	/**
	 * 锟斤拷锟斤拷值锟斤拷锟街凤拷转锟斤拷为锟斤拷锟街ｏ拷锟界将锟斤拷-65_-86_-73_-94_-69_-73_-66_-77转锟斤拷为 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
	 * 
	 * @param numString
	 * @return
	 */
	public static String getStringFormNum(String numString) {
		if (numString == null)
			return null;
		numString = numString.substring(1, numString.length() - 1);
		StringTokenizer stk = new StringTokenizer(numString, "_");
		byte[] num = new byte[stk.countTokens()];
		int index = 0;
		while (stk.hasMoreTokens()) {
			num[index++] = Byte.parseByte(stk.nextToken());
		}
		return new String(num);
	}

	/*public static void main(String[] args) {
		// String aaString="巡锟斤拷锟絴锟斤拷员锟斤拷锟斤拷锟絴锟借备锟斤拷锟斤拷锟斤拷|锟借备锟解房锟斤拷锟斤拷锟絴值锟斤拷锟絴值锟斤拷锟杰憋拷 ";
		String nString = farmmatStringToNum("锟借备锟斤拷锟斤拷锟斤拷");
		System.out.println(nString);
		String qq = getStringFormNum(nString);
		System.out.println(qq);
	}*/

	public static boolean isNull(String s) {
		if (s == null || "".equals(s)
				|| "null".equalsIgnoreCase(s.toLowerCase())
				|| "undefined".equals(s)) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 缁欏瓧绗︿覆璁惧畾榛樿鍊�
	 * 
	 * @Methods Name setDefultValue
	 * @Create In 2012-8-7 By qingsheng.zheng
	 * @param value
	 * @param defaultValue
	 *            void
	 */
	public String setDefultValue(String value, String defaultValue) {
		if (StringUtils.isNull(value)) {
			return defaultValue;
		} else
			return value;
	}

	public static String toString(Object o) {
		if (o == null)
			return null;
		else {
			String s = o.toString().trim();
			if ("".equals(s) || "null".equalsIgnoreCase(s)
					|| "undefined".equalsIgnoreCase(s))
				return null;
			return s;
		}
	}
	
	public List<String> toArray(String[] strs)
	{
		List<String> value=new ArrayList();
		for(int i=0;i<strs.length;i++)
		{
			value.add(strs[i]);
		}
		return value;
	}

}
